package com.example.moblie203zlw20216061300518;

import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private String UrL = "http://192.168.29.230:3000/profile";
    private TextView textView;
    private TextView textView2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OkHttpClient okHttpClient = new OkHttpClient();
        new Thread(new Runnable() {
            @Override
            public void run() {
                Request request = new Request.Builder().url(UrL).build();
                Call call = okHttpClient.newCall(request);//请求访问
                try {
                    Response response = call.execute();//同步获得响应
                    String name = response.body().string();
                    runOnUiThread(new Runnable() {//更新ui界面
                        @Override
                        public void run() {
                            try {
                                textView.setText(name);
                                JSONObject jsonObject=new JSONObject(name);
                                String name_2=jsonObject.optString("name");
                                textView2.setText(name_2);
                            }catch (JSONException e){
                               e.printStackTrace();
                            }
                        }
                    });
                }
                catch (Exception e) {
                }
            }
        }).start();
        initView();
    }
    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        textView2 = (TextView) findViewById(R.id.textView2);
    }
}